class Index{
    constructor(){
        //实例属性
        //获取相当元素对象
        //用户名
        this.us = document.querySelector('#us');
        //密码
        this.ps = document.querySelector('#ps');
        //登录按钮
        this.login = document.querySelector('#submit');
        //跳转按钮
        this.go = document.querySelector('#go');
        //添加需要的变量
        this.arr = [false,false];
        //添加事件
        this.addEvent();
    }
    //事件
    addEvent(){
         //记录this
         let that = this;
        //用户名（2，8的中文）
        this.us.onblur = function(){
            //规则
            let re = /^[\u4e00-\u9fa5a-zA-Z0-9]{6,12}$/;
            //检测内容
            let us = this.value;
            //进行检测
            if(re.test(us)){
                this.style.border = '2px  solid  #00FF00'
                this.style.boxShadow = '0px -5px 10px 0px  #00FF00'
                this.style.borderRadius = '10px';
                that.arr[0] = true;
            }else{
                this.style.border = '2px  solid  #CC0000'
                this.style.boxShadow = '0px -5px 10px 0px #CC0000'
                this.style.borderRadius = '10px';
                that.arr[0] = false;
            }
        }
        //密码 （2,32的字母、数字、下划线)
        this.ps.onblur = function(){
            //规则
            let re = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;
            //检测内容
            let ps = this.value;
            //进行检测
            if(re.test(ps)){
                this.style.border = '2px  solid  #00FF00'
                this.style.boxShadow = '0px -5px 10px 0px  #00FF00'
                this.style.borderRadius = '10px';
                that.arr[1] = true;
            }else{
                this.style.border = '2px  solid  #CC0000'
                this.style.boxShadow = '0px -5px 10px 0px #CC0000'
                this.style.borderRadius = '10px';
                that.arr[1] = false;
            }
        }
       
        //登录按钮
        this.login.onclick = function(){
            //检测开关
            if(that.arr.includes(false)){
                alert('密码错误');
            }else{
                //后端验证
                //获取当前用户名
                let us = that.us.value;
                //获取密码
                let ps = that.ps.value;
               
                //检测后端是否有我当前注册的用户
                //1. 获取后端数据
                //如果当前cookie中有数据，则返回字符串，如果没有数据，则返回undefeined,为了方便转为对象，则将undefined转为''
                let cookie_str = cookie.get('registors') ? cookie.get('registors') : '';
                //2. 转为对象
                let cookie_obj = cookie.convertStrToObj(cookie_str);
                //3. 检测用户名是否正确
                if(us in cookie_obj){
                    //用户名正确,判断密码是否正确
                    if(ps === cookie_obj[us]){
                        // alert('登录成功!');
                        location.href = '../index.html';

                        //七天免登录，创建一个cookie，7天有效期，将当前登录成功的用户名记录下来
                        cookie.set('logined',us,{expires : 7,path : '/'});
                    }else{
                        //密码错误
                        alert('密码不正确！');
                    }
                }else{
                    alert('用户不正确！');
                }
                
            }
        }
        // this.go.onclick = function(){
        //     location.href = './registor.html';
        // }
    }
}
new Index();